﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ServiceModel;

namespace PokerClient
{
    public partial class FormLogin : Form
    {
        private ServiceReference1.LoginClient proxy;
        bool closeApplication;
        public string LoginUsername;
        public FormLogin()
        {
            InitializeComponent();
            proxy = new ServiceReference1.LoginClient();
            closeApplication = true;
        }

        private void LoginBtn_Click(object sender, EventArgs e)
        {
            if (proxy.Login(textBox1.Text, textBox2.Text))
            {
                closeApplication = false;
                LoginUsername = textBox1.Text;
                this.Close();
            }
            else
            {
                MessageBox.Show("Username or password is incorrect!");
            }
        }

        private void RegistrationBtn_Click(object sender, EventArgs e)
        {
            //Resize form to show registration inputs and enable them
            this.Size = new Size(320, 400);
            RegistrationBtn.Enabled = false;
            RegisterBtn.Enabled = true;
            textBox3.Enabled = true;
            textBox4.Enabled = true;
            textBox5.Enabled = true;
        }

        private void RegisterBtn_Click(object sender, EventArgs e)
        {
            if (textBox4.Text == textBox5.Text)
            {
                if (proxy.Registration(textBox3.Text, textBox4.Text))
                {
                    MessageBox.Show("Registrated succefully");
                }
                else
                {
                    MessageBox.Show("Username already excist");
                }
            }
            else
            {
                MessageBox.Show("Password do not match");
            }
            
        }

        private void FormLogin_Closing(object sender, FormClosingEventArgs e)
        {

        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);

            if (e.CloseReason == CloseReason.WindowsShutDown
                    || e.CloseReason == CloseReason.ApplicationExitCall
                    || e.CloseReason == CloseReason.TaskManagerClosing)
            {
                return;
            }

            //Check if closing because logged in or pressed x
            switch (closeApplication)
            {
                case true:
                    Environment.Exit(0);
                    break;
                default:
                    break;
            }
        }
    }
}
